<?php namespace Home\Controller; 

use Hdphp\Controller\Controller;
use Home\Model\Login;
class LoginController extends Controller
{
	protected $db;

	//构造函数
	public function __init()
	{
		$this->db = new Login;
	}
	
    // 调用登录页面
    public function login(){
        
        View::make();
    }

    public function sign()
    {
    	if(IS_POST)
    	{
    		$user = $this->db->where('username',$_POST['username'])->first();
    		if($_POST['username'])
    		{
    			if($_POST['username'] != $user['username'])
    			{
    				View::error('账户名不存在或者账户名输入错误');
    				return false;
    			}
    		}
    		else{
    			if($_POST['username'] != $user['phone'])
    			{
    				View::error('账户名不存在或者账户名输入错误');
    				return false;
    			}
    		}
    		if(md5($_POST['password']) != $user['password'])
    		{
    			View::error('密码不正确');
    			return false;
    		}

    		$_SESSION['id'] = $user['id'];
    		$_SESSION['username'] = $user['username'];
    		$_SESSION['nickname'] = $user['nickname'];
    		// 成功后跳转首页
    	    go('Index/index');
    	}
    }

    public function cartSign()
    {
        if(IS_POST)
        {
            $user = $this->db->only($_POST['username']);
            if($_POST['username'])
            {
                if($_POST['username'] != $user['username'])
                {
                    View::ajax(array('message'=>'账户名不存在或者账户名输入错误'));
                    return false;
                }
            }
            else{
                if($_POST['username'] != $user['phone'])
                {
                    View::ajax(array('message'=>'账户名不存在或者账户名输入错误'));
                    return false;
                }
            }
            if(md5($_POST['password']) != $user['password'])
            {
                View::ajax(array('message'=>'密码不正确'));
                return false;
            }

            $_SESSION['id'] = $user['id'];
            $_SESSION['username'] = $user['username'];
            $_SESSION['nickname'] = $user['nickname'];
            View::ajax(array('message'=>'登錄成功,请继续购物')); 

        }
    }
}
